package com.jshx.sz.gdjt.activity;

import java.util.ArrayList;
import java.util.Map;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.jshx.sz.gdjt.R;
import com.jshx.sz.gdjt.bean.LoginBean;
import com.jshx.sz.gdjt.constant.ApplicationGlobal;
import com.jshx.sz.gdjt.control.HeaderView;
import com.jshx.sz.gdjt.thread.DataSynchronizedThread;
import com.jshx.sz.gdjt.thread.LoginDataSynchronizedThread;
import com.jshx.sz.gdjt.util.ProgressDialogUtil;
import com.jshx.sz.gdjt.util.ToastUtil;

/**
 * 主界面
 * @author caxiang
 * @time 2013-08-20
 */
public class MainActivity extends SuperActivity 
{
	/**
	 * 标题栏
	 */
	private HeaderView header;
	
	/**
	 * 主界面网格组件
	 */
	private GridView mainGrid;
	
	/**
	 * 图标数据
	 */
	private ArrayList<Integer> data;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		TAG = "MainActivity";
		findView();
		initView();
	}
	
	/**
	 * 实例化页面组件
	 */
	private void findView() 
	{
		header = (HeaderView)findViewById(R.id.main_header);
		mainGrid = (GridView)findViewById(R.id.main_grid);
	}
	
	
	/**
	 * 页面句柄
	 */
	private Handler handler_syn = new Handler(){
		@Override
		public void handleMessage(Message msg) 
		{
			if(msg.what == ApplicationGlobal.data_missed)
			{
				ProgressDialogUtil.dismiss();
				ToastUtil.showShortToast(MainActivity.this, "同步数据失败,请重新同步!!!");
			}
			else if(msg.what == ApplicationGlobal.data_obtained)
			{
				ProgressDialogUtil.dismiss();
				ToastUtil.showLongToast(MainActivity.this, "同步数据已完成!!!");
			}
			super.handleMessage(msg);
		}
	};
	

	/**
	 * 初始化页面数据和页面组件
	 */
	private void initView() 
	{
		header.setTitle("设备巡检");
	 
		header.setRightButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {


				 Dialog alertDialog = new AlertDialog.Builder(MainActivity.this) .
	                setTitle("数据同步"). 
	                setMessage("数据在后台同步中,请进行其他操作"). 
	                setPositiveButton("确定", new DialogInterface.OnClickListener() { 
	                     
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                    	DataSynchronizedThread thread=new DataSynchronizedThread(handler_syn);
	        				thread.start();
	                    } 
	                }).   create(); 
				 alertDialog.show(); 
				
				
				
				
			}
		}, R.drawable.titlebtn_select, "同步数据");
		

		data = new ArrayList<Integer>();
		
		
		data.add(R.drawable.tubiao01);
		data.add(R.drawable.tubiao02);
		data.add(R.drawable.tubiao03);
		data.add(R.drawable.tubiao04);
		data.add(R.drawable.tubiao05);
		data.add(R.drawable.tubiao06);
		data.add(R.drawable.tubiao07);
		
		mainGrid.setAdapter(myAdapter);
		mainGrid.setOnItemClickListener(new OnItemClickListener() 
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent();
				 
				switch (arg2) 
				{
					case 0:
						intent.setClass(MainActivity.this, TodoListActivity.class);
						intent.putExtra("type", "check");
						startActivity(intent);		
						break;
					case 1:
						intent.setClass(MainActivity.this, TodoListActivity.class);
						intent.putExtra("type", "examine");
						startActivity(intent);		
						break;
					case 2:
						intent.setClass(MainActivity.this, GetTaskListActivity.class);
						intent.putExtra("type","task");
						startActivity(intent);			
						break;
					case 3:
						intent.setClass(MainActivity.this, GetTaskListActivity.class);
						intent.putExtra("type", "reback");
						startActivity(intent);		
						break;
					case 4:
						intent.setClass(MainActivity.this, TodoListActivity.class);
						intent.putExtra("type", "checkHis");
						startActivity(intent);		
						break;
					case 5:
						intent.setClass(MainActivity.this, TodoListActivity.class);
						intent.putExtra("type", "examineHis");
						startActivity(intent);		
						break;
					case 6:
						intent.setClass(MainActivity.this, ToolsListActivity.class);
						intent.putExtra("type", "tool");
						startActivity(intent);		
						break;
					default:
						break;
				}
				
			}
		});
	}
	
	/**
	 * 
	 */
	private BaseAdapter myAdapter = new BaseAdapter() 
	{
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			convertView = new ImageView(MainActivity.this);
			((ImageView)convertView).setBackgroundResource(data.get(position));
			LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT
					, LayoutParams.WRAP_CONTENT);
			((ImageView)convertView).setLayoutParams(lp);
			return convertView;
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public Object getItem(int position) {
			return data.get(position);
		}
		
		@Override
		public int getCount() {
			return data.size();
		}
	};
}
